package com.jixiantubu.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gemptc.bean.Photo_Library;
import com.jixiantubu.entity.Post_InfoDaoimp;
import com.jixiantubu.entity.TupianDaoimp;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

/**
 * Servlet implementation class Tupian_jieshou
 */
@WebServlet("/Tupian_jieshou")
public class Tupian_jieshou extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Tupian_jieshou() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter printWriter = response.getWriter();
		boolean a;
		SmartUpload smartUpload = new SmartUpload();
		smartUpload.initialize(this.getServletConfig(), request, response);
		com.jspsmart.upload.Files files = smartUpload.getFiles();
		List<Photo_Library> imgs = new ArrayList<Photo_Library>();
		try {
			smartUpload.upload();
			String phname = smartUpload.getRequest().getParameter("phname");
			String user_id = smartUpload.getRequest().getParameter("user_id");
			// 得到上传的t_time
			Date currentTime = new Date();
			String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
					.format(currentTime);
			// 得到手机端传来的t_hidden
			String st = smartUpload.getRequest().getParameter("flag");
			if (st.equals("true")) {
				a = true;

			} else {
				a = false;
			}
			boolean phhidden = a;
			System.out.println(phname + "--" + user_id + "--" + date + "--"
					+ phhidden);
			TupianDaoimp tupian=new TupianDaoimp();
			boolean isSuccess = tupian.insertPhoto(Integer.parseInt(user_id), phname, date, phhidden);
			if (isSuccess) {
				printWriter.write("success");
				Post_InfoDaoimp post = new Post_InfoDaoimp();
				int phid=post.selectPhid(date);
				for (int i = 0; i < files.getCount(); i++) {
					com.jspsmart.upload.File file = files.getFile(i);
					if (!file.isMissing()) {
							SimpleDateFormat sdf = new SimpleDateFormat(
									"yyyyMMddHHmmssSSS");
							String pl_path = sdf.format(new java.util.Date());
							pl_path = pl_path + "." + file.getFileExt();// 得到文件的扩展名
							String filename = "D:\\workspace\\Jinnang7\\WebContent\\img\\"
									+ pl_path;
							System.out.println("文件路径：" + filename);
							file.saveAs(filename);
							//imgs.add(new Photo_Library(Integer.parseInt(user_id), phid, pl_path));
							imgs.add(new Photo_Library(Integer.parseInt(user_id), phid, 0, pl_path));
							System.out.println("胡蛟龙------"+imgs.toString());
							
						} 						
					}	
				post.insertPhotoLibary(imgs);
			}
		} catch (SmartUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
